//
//  ThingsViewController.m
//  糗事百科
//
//  Created by lanou on 16/5/28.
//  Copyright © 2016年 刘恒春. All rights reserved.
//

#import "ThingsViewController.h"
//图文页面
#import "ThingsSuggestTableViewController.h"
//最新页面
#import "ThingslaestTableViewController.h"
//纯文页面
#import "ThingstextTableViewController.h"
//纯图页面
#import "ThingsimgrankTableViewController.h"
//精华页面
#import "ThingsdayTableViewController.h"
//网络请求
#import "RequestManager.h"
//导航栏Model
#import "ThingsConfigModel.h"
//导航栏view
#import "UIView+XCMExtension.h"
#import "ThingsNavbtn.h"

@interface ThingsViewController ()<UIScrollViewDelegate>
//将各个子视图控制器设置为属性
@property (nonatomic,retain)ThingsSuggestTableViewController *suggestVc;
@property (nonatomic,retain)ThingslaestTableViewController *lataseVc;
@property (nonatomic,retain)ThingstextTableViewController *textVc;
@property (nonatomic,retain)ThingsimgrankTableViewController *imgVc;
@property (nonatomic,retain)ThingsdayTableViewController *dayVc;

@property (nonatomic,retain)NSMutableArray *titleArray;
@property (nonatomic,retain)NSMutableArray *urlArray;
@property (nonatomic,retain)UIScrollView *contentView;
@property (weak, nonatomic) UIView *titlesView;
@property (weak, nonatomic) ThingsNavbtn *selectedButton;
@property (weak, nonatomic) UIView *sliderView;
@property (strong, nonatomic) MASConstraint *sliderViewCenterX;
@end

@implementation ThingsViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    //数组初始化
    _titleArray = [NSMutableArray array];
    _urlArray = [NSMutableArray array];
    self.navigationController.navigationBar.translucent=NO;
//    请求数据
    [self netrequset];
    //设置内容视图
    [self setupContentView];
    //设置导航栏
    [self setupNavBar];
    //设置标题视图
    UIView *titlesView = [[UIView alloc] init];
    titlesView.backgroundColor = [UIColor whiteColor];
    titlesView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    titlesView.frame = CGRectMake(0, 0, self.view.frame.size.width, 40);
    [self.view addSubview:titlesView];
    self.titlesView = titlesView;

}
#pragma mark---------网络请求----
-(void)netrequset
{
    [RequestManager initWithUrlString:knavurl requestType:@"GET" parDic:nil finishBlock:^(NSData *data)
     {
         NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
         NSArray *listArray = dataDic[@"articleCofig"];
         
         for (NSDictionary *dic in listArray)
         {
             ThingsConfigModel *model =[[ThingsConfigModel alloc]init];
             [model setValuesForKeysWithDictionary:dic];
             [_titleArray addObject:model.title];
             [_urlArray addObject:model.url];
         }
         dispatch_async(dispatch_get_main_queue(), ^
         {
             [self creatChildViewController];
             [self setupTitlesView];
            
         });
         NSLog(@"导航请求成功");
         
     } errorBlock:^(NSError *error)
     {
         NSLog(@"导航请求失败");
     }];

}
#pragma mark---------添加子视图控制器的方法----
-(void)creatChildViewController
{
    
    //图文
    _suggestVc=[[ThingsSuggestTableViewController alloc] init];
    _suggestVc.SuggestUrl =_urlArray[0];
    _suggestVc.view.backgroundColor = GlobalColor;
    _suggestVc.view.frame=CGRectMake(kWidth*0, 0, kWidth, kHeight-30);
    [self addChildViewController:_suggestVc];
    [_contentView addSubview:_suggestVc.view];
    //纯文
    _textVc=[[ThingstextTableViewController alloc] init];
    _textVc.textUrl=_urlArray[1];
    _textVc.view.backgroundColor=GlobalColor;
    _textVc.view.frame=CGRectMake(kWidth*1, 0, kWidth, kHeight-30);
    [self addChildViewController:_textVc];
    [_contentView addSubview:_textVc.view];
    //纯图
    _imgVc=[[ThingsimgrankTableViewController alloc] init];
    _imgVc.imgrankUrl=_urlArray[2];
    _imgVc.view.backgroundColor=GlobalColor;
    _imgVc.view.frame=CGRectMake(kWidth*2, 0, kWidth, kHeight-30);
    [self addChildViewController:_imgVc];
    [_contentView addSubview:_imgVc.view];
    //最新
    _lataseVc=[[ThingslaestTableViewController alloc] init];
    _lataseVc.laestUrl=_urlArray[3];
    _lataseVc.view.backgroundColor=GlobalColor;
    _lataseVc.view.frame=CGRectMake(kWidth*3, 0, kWidth, kHeight-30);
    [self addChildViewController:_lataseVc];
    [_contentView addSubview:_lataseVc.view];
    //精华
    _dayVc=[[ThingsdayTableViewController alloc] init];
    _dayVc.dayUrl=_urlArray[4];
    _dayVc.view.backgroundColor=GlobalColor;
    _dayVc.view.frame=CGRectMake(kWidth*4, 0, kWidth, kHeight-30);
    [self addChildViewController:_dayVc];
    [_contentView addSubview:_dayVc.view];
    
}
#pragma mark---------设置内容视图---------
- (void)setupContentView
{
    UIScrollView *contentView = [[UIScrollView alloc] init];
    contentView.backgroundColor = GlobalColor;
    contentView.frame = CGRectMake(0, 40, kWidth, kHeight-30);
    //UIViewAutoresizingFlexibleWidth 自动调整自己的宽度，保证与superView左边和右边的距离不变。
    //UIViewAutoresizingFlexibleHeight 自动调整自己的高度，保证与superView顶部和底部的距离不变。
    contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    contentView.delegate = self;
    contentView.showsHorizontalScrollIndicator = NO;
    contentView.showsVerticalScrollIndicator = NO;
    contentView.pagingEnabled = YES;
    contentView.contentSize = CGSizeMake(contentView.xcm_width * 5, 0);
    [self.view addSubview:contentView];
    self.contentView = contentView;
}
#pragma mark---------设置导航栏----------
- (void)setupNavBar
{
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"about_logo"]];
    self.navigationItem.titleView.frame =CGRectMake(0, 0, 0, 30);
}
#pragma mark---------设置标题视图------------
- (void)setupTitlesView
{
    //创建5个按钮
    ThingsNavbtn *button = [self setupTitleButton:_titleArray[0]];
    for (int i = 1; i < _titleArray.count; i ++)
    {
        [self setupTitleButton:_titleArray[i]];
    }
    [self titleClick:button];
    [self switchController:0];
    
    // 底部滑条
    UIView *sliderView = [[UIView alloc] init];
    sliderView.backgroundColor = [UIColor orangeColor];
    [self.titlesView addSubview:sliderView];
    self.sliderView = sliderView;
    
    [sliderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(25, 3));
        make.bottom.equalTo(self.titlesView);
        self.sliderViewCenterX = make.centerX.equalTo(button);
    }];
}
#pragma mark---------网络请求----
- (ThingsNavbtn *)setupTitleButton:(NSString *)title
{
    ThingsNavbtn *button = [ThingsNavbtn buttonWithType:UIButtonTypeCustom];
    [button setTitle:title forState:UIControlStateNormal];
    [button addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.titlesView addSubview:button];
    
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.titlesView).multipliedBy(0.2);
        make.top.bottom.equalTo(self.titlesView);
        NSUInteger index = self.titlesView.subviews.count - 1;
        if (index == 0) {
            make.left.equalTo(self.titlesView);
        } else {
            make.left.equalTo(self.titlesView.subviews[index - 1].mas_right);
        }
    }];
    
    return button;
}
- (void)titleClick:(ThingsNavbtn *)button
{
    self.selectedButton.enabled = YES;
    button.enabled = NO;
    self.selectedButton = button;
    
    // 消除约束
    [self.sliderViewCenterX uninstall];
    self.sliderViewCenterX = nil;
    
    // 添加约束
    [self.sliderView mas_makeConstraints:^(MASConstraintMaker *make) {
        self.sliderViewCenterX = make.centerX.equalTo(button);
    }];
    int index = (int)[self.titlesView.subviews indexOfObject:button];
    [self.contentView setContentOffset:CGPointMake(index * self.contentView.frame.size.width, self.contentView.contentOffset.y) animated:YES];
}

- (void)switchController:(int)index
{
    UIViewController *vc = self.childViewControllers[index];
    vc.view.xcm_y = 0;
    vc.view.xcm_width = self.contentView.xcm_width;
    vc.view.xcm_height = self.contentView.xcm_height;
    vc.view.xcm_x = vc.view.xcm_width * index;
    [self.contentView addSubview:vc.view];
}

#pragma mark - <UIScrollViewDelegate>
- (void)scrollViewDidEndDecelerating:(nonnull UIScrollView *)scrollView
{
    int index = scrollView.contentOffset.x / scrollView.frame.size.width;
    [self titleClick:self.titlesView.subviews[index]];
    [self switchController:index];
}

- (void)scrollViewDidEndScrollingAnimation:(nonnull UIScrollView *)scrollView
{
    [self switchController:(int)(scrollView.contentOffset.x / scrollView.frame.size.width)];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
